/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.controllers;

import bl.controllers.JAXBController;
import bl.controllers.UserJpaController;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import entities.AreaType;
import entities.CvType;
import entities.Document;
import entities.DocumentType;
import entities.JobType;
import entities.LanguageRecordType;
import entities.MscType;
import entities.PersonalInfoType;
import entities.ProjectType;
import entities.TitleType;
import entities.User;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

/**
 *
 * @author Spyros
 */
public class FormController {

    private JAXBController jaxbc;
    private Integer userId;
    private DocumentType documentType;
    private Boolean tempSaved = false;
    private Integer documentId;

    public FormController(Integer userId) {
        jaxbc = new JAXBController(userId);
        this.userId = userId;
    }

    public boolean isOutOfDate() {
        //TODO check if current date is in period
        return false;
    }

    public boolean isTempSaved() {
        HashMap<Integer,DocumentType> hashMap = jaxbc.getTempDocument();
        for (Iterator it = hashMap.keySet().iterator(); it.hasNext();) {
            Integer i = (Integer) it.next();
            documentId = i;
            documentType = hashMap.get(i);
        }
        tempSaved = (documentType != null);
        return tempSaved;
    }

    public boolean isCommited() {
        return jaxbc.isCommited();
    }

    public Byte getAreaOfStudyValue(String area){
        if (tempSaved){
            for (Iterator it = documentType.getAreasOfStudy().getArea().iterator(); it.hasNext();) {
                AreaType areaType = (AreaType) it.next();
                if( areaType.getName().equals(area)){
                    return areaType.getValue();
                }
            }
            return null;
        }else{
            return null;
        }
    }

    public BeanItem getPersonalInfoDataSource() {
        PersonalInfoType personalInfoType;
        if (tempSaved) {
            personalInfoType = documentType.getPersonalInfo();
        } else {
            personalInfoType = new PersonalInfoType();
        }
        BeanItem beanItem = new BeanItem(personalInfoType);
        return beanItem;
    }

    public BeanItemContainer getStudiesBeanItemContainer() {
        if (tempSaved) {
            return new BeanItemContainer(TitleType.class, documentType.getStudies().getTitle());
        }
        return new BeanItemContainer(TitleType.class);
    }

    public BeanItemContainer getProjectsBeanItemContainer() {
        if (tempSaved) {
            return new BeanItemContainer(ProjectType.class, documentType.getProjects().getProject());
        }
        return new BeanItemContainer(ProjectType.class);
    }

    public BeanItemContainer getLanguagesBeanItemContainer() {
        if (tempSaved) {
            return new BeanItemContainer(LanguageRecordType.class, documentType.getLanguages().getLanguageRecord());
        }
        return new BeanItemContainer(LanguageRecordType.class);
    }

    public BeanItemContainer getJobsBeanItemContainer() {
                if (tempSaved) {
            return new BeanItemContainer(JobType.class, documentType.getJobs().getJob());
        }
        return new BeanItemContainer(JobType.class);
    }

    public BeanItemContainer getCvsBeanItemContainer() {
                        if (tempSaved) {
            return new BeanItemContainer(CvType.class, documentType.getCvs().getCv());
        }
        return new BeanItemContainer(CvType.class);
    }

    public BeanItemContainer getMscBeanItemContainer() {
        if (tempSaved) {
            return new BeanItemContainer(MscType.class, documentType.getOtherMsc().getMsc());
        }
        return new BeanItemContainer(MscType.class);
    }

    public void setAreasOfStudy(String area1Value, String area2Value, String area3Value) {
        jaxbc.setAreasOfStudy(area1Value, area2Value, area3Value);
    }

    public void setPersonalInfo(Item personalInfoTypeItem) {
        PersonalInfoType personalInfoType = (PersonalInfoType) ((BeanItem) personalInfoTypeItem).getBean();
        jaxbc.setPersonalInfo(personalInfoType);
    }

    public void setStudies(Collection<TitleType> titleTypes) {
        jaxbc.setStudies(titleTypes);
    }

    public void setProjects(Collection<ProjectType> projectTypes) {
        jaxbc.setProjects(projectTypes);
    }

    public void setAwards(Collection<String> awards) {
        jaxbc.setAwards(awards);
    }

    public void setLanguages(Collection<LanguageRecordType> languageRecordTypes) {
        jaxbc.setLanguages(languageRecordTypes);
    }

    public void setJobs(Collection<JobType> jobTypes) {
        jaxbc.setJobs(jobTypes);
    }

    public void setResearch(Collection<String> research) {
        jaxbc.setResearch(research);
    }

    public void setCvs(Collection<CvType> cvTypes) {
        jaxbc.setCvs(cvTypes);
    }

    public void setOtherMsc(Collection<MscType> mscTypes) {
        jaxbc.setOtherMsc(mscTypes);
    }

    public void setOtherInfo(String value) {
        jaxbc.setOtherInfo(value);
    }

    public void save() {
        Document document = new Document();
        if (documentId != null){
            document.setId(documentId);
        }
        UserJpaController userJpaController = new UserJpaController();
        document.setUser(userJpaController.findUser(userId));
        jaxbc.save(document);
    }

    public void submit() {
        save();
        UserJpaController userJpaController = new UserJpaController();
        User user = userJpaController.findUser(userId);
        user.setSubmit(true);
        try {
            userJpaController.edit(user);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }
}
